<?php
class expand_modelModel extends baseModel{
    protected $table = 'expand_model';

    //列表
    public function model_list() {
        return $this->select('','','mid asc');
    }

    //获取模型信息
    public function table_info($table,$mid=null) {
        $where="`table`='".$table."'";
        if(!empty($mid)){
        $where.=' AND mid<>'.$mid;
        }
        return $this->find($where);
    }

    //获取模型信息
    public function info($mid) {
        return $this->find('mid='.$mid);
    }

    //添加
    public function add($data) {
        //建立模型添加表
        $sql="
        CREATE TABLE IF NOT EXISTS `{$this->model->pre}expand_content_{$data['table']}` (
          `id` int(10) NOT NULL AUTO_INCREMENT,
          `aid` int(10) DEFAULT NULL,
          PRIMARY KEY (`id`)
        ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
        ";
        $this->query($sql);
        return $this->insert($data);
    }

    //修改
    public function edit($data) {
        $info=$this->info($data['mid']);
        //修改模型表
        $sql="
        ALTER TABLE {$this->model->pre}expand_content_{$info['table']} RENAME TO {$this->model->pre}expand_content_{$data['table']}
        ";
        $this->model->query($sql);
        //修改信息
        $condition['mid']=intval($data['mid']);
        return $this->update($condition,$data); 
    }

    //删除
    public function del($data)
    {
        $info=$this->info($data['mid']);
        //删除表
        $sql="
        DROP TABLE `{$this->model->pre}expand_content_{$info['table']}`
        ";
        $this->model->query($sql);
        //删除表内字段
        model('expand_model_field')->model_del($data['mid']);
        return $this->delete('mid='.$data['mid']); 
    }

    //APIHTML
    public function model_list_html($info=array())
    {
        $model_list=$this->model_list();
        $html='
                <div class="formitm">
                    <label class="lab" >扩展模型：</label>
                    <div class="ipt">
                        <div class="txt">
                            <select name="expand" id="expand">
                                <option value="0">无</option>';
        if(!empty($model_list)){
            foreach ($model_list as $vo) {
                if($vo['mid']==$info['expand']){
                    $html.='<option value="'.$vo['mid'].'" selected="selected">'.$vo['name'].'</option>';
                }else{
                    $html.='<option value="'.$vo['mid'].'" >'.$vo['name'].'</option>';
                }
            }
        }
        $html.='</select>
                        </div>
                        <span class="Validform_checktip">用于增加栏目下内容的附加信息</span> </div>
                </div>
        ';
        return $html;
    }

    //内容信息头部html
    public function model_info_html($info=array())
    {
        $html='<div id="filter_list"></div>';
        return $html;
    }

    //内容栏目选择JS
    public function model_info_js_class($info=array())
    {
        $html='
        $.post("'.url('expand_model/admin_list/get_field').'",{cid: cid, aid: aid},function(data){
            if(!data){
                $("#expand").html(\'\');
            }else{
                $("#expand").html(data);
            }
        },"html");
        ';
        return $html;
    }

    public function model_list_sql($mid=null)
    {
        if(empty($mid)){
            return;
        }
        $model_info=$this->info($mid);
        $expand=" LEFT JOIN {$this->model->pre}expand_content_{$model_info['table']} C ON C.aid = A.aid ";
        $expand_field="C.*,";
        return array('sql'=>$expand,'field'=>$expand_field);
    }

    public function label_model_list_sql($data=array())
    {
        $data['cid']=intval($data['cid']);
        if(is_int($data['cid'])||!empty($data['expand'])){
            if(empty($data['expand'])){
                $category=$this->model->table('category')->where('cid='.$data['cid'])->find();
                $expand_id=$category['expand'];
            }else{
                $expand_id=$data['expand'];
            }
            if(!empty($expand_id)){
                $model_info=$this->model->table('expand_model')->where('mid='.$expand_id)->find();
                if(!empty($model_info)){
                    $expand=" LEFT JOIN {$this->model->pre}expand_content_{$model_info['table']} C ON C.aid = A.aid ";
                    $expand_field="C.*,";
                }
                
            }
        }
        return array('sql'=>$expand,'field'=>$expand_field);

    }

     

}

?>